package learn_bouncycastle.security;

import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Arrays;
import java.util.Base64;
import java.util.Base64.Decoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class T1 {
	public static void main(String[] args) {
		String encryptedData = "xhs2D3KqbELAzjITdijGmDyZQNlgA8uJQQ8jGHkVL8UUrgnyDZmGE58vOtRL3il4dlGLNGrwnMdej/wN0u928r+ILPFqlz00Qbq8YLv3m5QhcgXdw5xtALWzMP0A4rmcqI91W/yeSRkX9ksKXCs6+v8imkgTdWQm9E3Ea81Xoq3BBXr2g0+O0Rguz5cM0hy0GgOc4yEiwjycBa6WFVbXiR5KQo1Jz1eL5SEM9HifMmwtifMuoS805M066/YbhFwPuLVRdn2ZJqBy94sTLNCXWCjJlUbq5HnJMxPnbatuawYtFK4LCOQU8dKv4lDsiCvYpWUXJdHurQY6whY8UQ03wfTj1ukgdBoPYmTdt8hkj2NVzU9lFG5wvbHBDBLE27kqG8Dvy6zoH33XcCn2jJHNmrgn0Misw5q4ebTS78HbX8vMGKkY3+fC9EhRkIsnOrGwPikhvB/vZzJp8wmI0vMH5g==";
		String iv = "E1OHcFOZ2wwgKVv8DFyXnw==";
//		String sessionKey = "eG7vN3Ksfcpwlzb3RuBfbg==";
		String sessionKey= "EVtybIPevBuS5KleOMNoyA==";
		
		String encryptedData2 = "a8mxwxCAFc2e1hWtu67jE2noMmpXYIQm6N8z+oZuU5m/3Z5lgcNo/CEwv1dn+HOjlD45o7ffRPWR6w5t1UNzB6GXBrOVuXBpC9LYu/zVsIwGRcXrg8/X393dqerzPrwZIc2L3s6rdhRw7jdpdBadMiwqr1I5kN/du2JXTtkjLuvwUU82xtpznuwU4rp0oXC7gGpZ7RWL8kQ0T11FQlN5BHXwWr/VyiUUbnU4YQKgrFkf3kdKXb14MQcppo9/SEO34EhgDKFzegfJBp7j1TV/1vtdey1mslIJbyBNr3u+LNrny+efwisB6QNe93d6SjHfcgDriLkv5sOPC3T8nBxKPbpNl0XUIKI1rjkESckoN+GRnXJUMGKq/aRhh3q29RSGH1Atg8HHP5n+4cqddVsTpQ3ADtte2qxWuSMPqtd4diGXfMGrbjgwo+ygQkNrKVGi/kCfsHfPHWcBAtyghDTJIg==";
		String iv2 = "YvgsZIpj5la76i+cOCGTbQ==";
		String sessionKey2= "EVtybIPevBuS5KleOMNoyA==";
		String result = decryptUserInfo(encryptedData, iv, sessionKey);
//		String result = decryptUserInfo(encryptedData2, iv2, sessionKey2);
		System.out.println(result);
		
		// Ulg4DGDf0u4y3xfsLxHwyA==
		// B6ZAMl11dxMh9U77j1S0Bg==
		// 2viIhOn5NyJkaP3SRYNbxw==
	}
	

	/**
	 * 解密用户敏感数据获取用户信息
	 *
	 * @param sessionKey    数据进行加密签名的密钥
	 * @param encryptedData 包括敏感数据在内的完整用户信息的加密数据
	 * @param iv            加密算法的初始向量
	 * @return
	 */
	public static String decryptUserInfo(String encryptedData, String iv, String sessionKey) {
		Decoder decoder = Base64.getDecoder();

		byte[] dataByte = decoder.decode(encryptedData);
		byte[] keyByte = decoder.decode(sessionKey);
		System.out.println("keyByte的字节数：" + keyByte.length);
		byte[] ivByte = decoder.decode(iv);

		try {
			// 如果密钥不足16位，那么就补足
			int base = 16;
			if (keyByte.length % base != 0) {
				int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
				byte[] temp = new byte[groups * base];
				Arrays.fill(temp, (byte) 0);
				System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
				keyByte = temp;
			}
			// 初始化
			Security.addProvider(new BouncyCastleProvider());
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
			SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
			AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
			parameters.init(new IvParameterSpec(ivByte));
			cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
			byte[] resultByte = cipher.doFinal(dataByte);
			
			if (null != resultByte && resultByte.length > 0) {
				return new String(resultByte, "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}



